<?php

namespace app\api\model\group;

use app\api\model\order\OrderMusicDetailApiModel;
use app\logic\model\group\GroupGoodsLogicModel;
use app\logic\single\GoodsSingle;
use app\logic\single\MusicSingle;

class GroupGoodsApiModel extends GroupGoodsLogicModel
{

    public function getApiList(array $param,$where=[],$filed="*",$page=1,$limit=15,$order="id desc"){
        $query = $this->where($where)->where(function ($q) use ($param){
            if(check_request($param,'show_start_time')){
                $q->where('show_start_time','<=',$param['show_start_time']);
            }
            if(check_request($param,'show_end_time')){
                $q->where('show_end_time','>=',$param['show_end_time']);
            }
        })->field($filed)->order($order);
        $count = $query->count();
        $list = $query->limit($limit)
            ->select();
        if($count >=1){
            foreach ($list as &$val){
                $val['goods'] = GoodsSingle::getInstance()->getGoods($val['goods_id'],$val['type']);
                $val['num'] = GroupOrderApiModel::getApiBuyNum($val,$val['id']);
            }
            unset($val);
        }
        return [$list,$count];
    }

    public function details($id, string $field)
    {

        $info = $this->getFindById($id,$field);
        if(empty($info)){
            return false;
        }

        $info['num'] = GroupOrderApiModel::getApiBuyNum($info,$info['id']);
        return $info;
    }
}